﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 飞机大战
{
    class SingleObject
    {
        /// <summary>
        /// 单例设计模式
        /// </summary>

        //1.构造函数私有化
        private SingleObject()
        { }

        //2.声明全局唯一的对象
        private static SingleObject _single = null;

        //3.提供一个静态函数用于返回一个唯一的对象
        public static SingleObject GetSingle()
        {
            if (_single == null)
            {
                _single = new SingleObject();
            }
            return _single;
        }

        /// <summary>
        /// 玩家飞机被击中的次数
        /// </summary>
        public int over_count = 0;

        /// <summary>
        /// 存储背景在游戏中唯一的对象
        /// </summary>
        public BackGround BG { get; set; }

        /// <summary>
        /// 存储玩家飞机在游戏中唯一的对象
        /// </summary>
        public PlaneHero PH { get; set; }

        /// <summary>
        /// 声明一个集合对象用来存储玩家子弹
        /// </summary>
        List<HeroBullet> listHeroBullet = new List<HeroBullet>();

        /// <summary>
        /// 声明一个集合对象来存储敌人飞机对象
        /// </summary>
        public List<PlaneEnemy> ListPlaneEnemy = new List<PlaneEnemy>();

        /// <summary>
        /// 声明一个集合来存储敌人子弹对象
        /// </summary>
        List<EnemyBullet> listEnemyBullet = new List<EnemyBullet>();

        /// <summary>
        /// 声明一个集合来存储敌人爆炸的对象
        /// </summary>
        List<EnemyBoom> listEnemyBoom = new List<EnemyBoom>();

        /// <summary>
        /// 声明一个集合来存储玩家爆炸的对象
        /// </summary>
        List<HeroBoom> ListHeroBoom = new List<HeroBoom>();

        /// <summary>
        /// 将创建的游戏对象添加到窗体中
        /// </summary>
        /// <param name="go"></param>
        public void AddGameObject(Game go)
        {
            if (go is BackGround)
            {
                this.BG = go as BackGround;
            }
            else if (go is PlaneHero)
            {
                this.PH = go as PlaneHero;
            }
            else if (go is HeroBullet)
            {
                listHeroBullet.Add(go as HeroBullet);
            }
            else if (go is PlaneEnemy)
            {
                ListPlaneEnemy.Add(go as PlaneEnemy);
            }
            else if (go is EnemyBoom)
            {
                listEnemyBoom.Add(go as EnemyBoom);
            }
            else if (go is EnemyBullet)
            {
                listEnemyBullet.Add(go as EnemyBullet);
            }
            else if (go is HeroBoom)
            {
                ListHeroBoom.Add(go as HeroBoom);
            }
        }

        /// <summary>
        /// 将游戏对象从游戏中移除
        /// </summary>
        /// <param name="go"></param>
        public void RemoveGameObject(Game go)
        {
            if (go is PlaneEnemy)
            {
                ListPlaneEnemy.Remove(go as PlaneEnemy);
            }
            else if (go is HeroBullet)
            {
                listHeroBullet.Remove(go as HeroBullet);
            }
            else if (go is EnemyBoom)
            {
                listEnemyBoom.Remove(go as EnemyBoom);
            }
            else if (go is EnemyBullet)
            {
                listEnemyBullet.Remove(go as EnemyBullet);
            }
            else if (go is HeroBoom)
            {
                ListHeroBoom.Remove(go as HeroBoom);
            }
        }

        public void Draw(Graphics g)
        {
            //绘制背景
            this.BG.Draw(g);

            //绘制玩家飞机
            this.PH.Draw(g);

            for (int i = 0; i < listHeroBullet.Count; i++)
            {
                listHeroBullet[i].Draw(g);
            }
            for (int i = 0; i < ListPlaneEnemy.Count; i++)
            {
                ListPlaneEnemy[i].Draw(g);
            }
            for (int i = 0; i < listEnemyBoom.Count; i++)
            {
                listEnemyBoom[i].Draw(g);
            }
            for (int i = 0; i < listEnemyBullet.Count; i++)
            {
                listEnemyBullet[i].Draw(g);
            }
            for (int i = 0; i < ListHeroBoom.Count; i++)
            {
                ListHeroBoom[i].Draw(g);
            }
        }

        /// <summary>
        /// 记录玩家分数
        /// </summary>
        public int Score { get; set; }

        /// <summary>
        /// 碰撞检测
        /// </summary>
        public void PZJC()
        {
            #region 判断玩家的子弹是否打到敌人
            for (int i = 0; i < listHeroBullet.Count; i++)
            {
                for (int j = 0; j < ListPlaneEnemy.Count; j++)
                {
                    //如果二者矩形有交集，即发生碰撞
                    if (listHeroBullet[i].GetRectangle().IntersectsWith(ListPlaneEnemy[j].GetRectangle()))
                    {
                        //敌机生命值减少
                        ListPlaneEnemy[j].Life -= listHeroBullet[i].power;

                        //判断敌机是否死亡
                        ListPlaneEnemy[j].IsOver();

                        //子弹打到敌机后销毁
                        listHeroBullet.Remove(listHeroBullet[i]);
                        break;
                    }
                }
            }
            #endregion

            #region 判断敌人的子弹是否打到玩家
            for (int i = 0; i < listEnemyBullet.Count; i++)
            {
                if (listEnemyBullet[i].GetRectangle().IntersectsWith(this.PH.GetRectangle()))
                {
                    //玩家飞机爆炸但不死亡
                    this.PH.IsOver();
                    over_count++;
                    break;
                }
            }
            #endregion

            #region 判断玩家是否和敌人飞机发生相撞
            for (int i = 0; i < ListPlaneEnemy.Count; i++)
            {
                if (ListPlaneEnemy[i].GetRectangle().IntersectsWith(this.PH.GetRectangle()))
                {
                    ListPlaneEnemy[i].Life = 0;
                    ListPlaneEnemy[i].IsOver();
                }
            }
            #endregion
        }
    }
}
